package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.AccountantDetailModel;
import com.banshuibao.bsb_android.util.WebViewCssUtil;

/**
 * Created by zyh on 2018/5/29.
 */

public class AccountantDetailActivity extends BaseActivity {

    private TextView number, range, title;

    private WebView webView;

    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accountant_detail);

        initData();
        initView();
        loadData();
    }

    private void initData() {
        id = getIntent().getIntExtra("id", 0);
    }


    private void initView() {
        title = findViewById(R.id.title_text);
        number = findViewById(R.id.number);
        range = findViewById(R.id.range);
        webView = findViewById(R.id.webView);
        ///WebViewUtil.addImageClickListener(this, webView);
    }

    private void loadData() {
        AccountantDetailModel.Body body = new AccountantDetailModel.Body();
        body.setId(id);
        HttpUtil.doPost(Constants.Url.ACCOUNTANT_DETAILS, body, new HttpResponse(this, AccountantDetailModel.class) {
            @Override
            public void onResult(Object obj) {
                AccountantDetailModel model = (AccountantDetailModel) obj;
                AccountantDetailModel.Data data = model.getData();
                if (webView != null) {
                    String name = data.getName();
                    if (name.length() > 12) {
                        name = name.substring(0, 12) + "...";
                    }
                    title.setText(name);
                    number.setText(String.valueOf("编码: " + data.getCode()));
                    range.setText(String.valueOf("适用范围: " + data.getScope()));
                    String newHtml = WebViewCssUtil.setCss(data.getContent());
                    webView.loadDataWithBaseURL(null, newHtml, "text/html", "utf-8", null);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.destroy();
            webView = null;
        }
    }
}
